/*
 * @Author: wwssaabb
 * @Date: 2021-12-10 17:03:09
 * @LastEditTime: 2021-12-10 17:36:16
 * @FilePath: \handwritten-code\main\字符串模板.js
 */

//字符串模板 

function render(template, data) {
  //获取匹配到的替换字符串
  let match = template.match(/(\{\{\w+\}\})/g)
  if (!match) return template

  //遍历match
  match.forEach(s => {
    let key = s.slice(2, -2)
    if (key) {
      template = template.replace(s, data[key])
    }
  })
  return template
}


let str = '我是{{name}}，年龄{{age}}，性别{{sex}}'
let data = {
  name: '三金',
  age: 18,
  sex: '男'
}
console.log(render(str, data))